/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhuangfei.ohos_timetableview.spinner;

import com.zhuangfei.ohos_timetableview.ResourceTable;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.configuration.Configuration;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.List;

/**
 * CameraAbilitySlice
 *
 * @param <T> MaterialSpinnerBaseAdapter
 * @since 2021-03-29
 */
public abstract class MaterialSpinnerBaseAdapter<T> extends RecycleItemProvider {
    static final HiLogLabel LOG_LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MaterialSpinnerBaseAdapter");

    private final Context context;
    private int selectedIndex;
    private int textColor;
    private int textSize;
    private int backgroundSelector;
    private int popupPaddingTop;
    private int popupPaddingLeft;
    private int popupPaddingBottom;
    private int popupPaddingRight;
    private boolean isHintEnabled;

    /**
     * MaterialSpinnerBaseAdapter
     *
     * @param context
     */
    public MaterialSpinnerBaseAdapter(Context context) {
        this.context = context;
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {
        final Text textView;
        if (convertView == null) {
            LayoutScatter inflater = LayoutScatter.getInstance(context);
            convertView = inflater.parse(ResourceTable.Layout_ms__list_item, parent, false);
            textView = (Text) convertView.findComponentById(ResourceTable.Id_tv_tinted_spinner);
            textView.setTextColor(new Color(textColor));
            textView.setTextSize(textSize);
            HiLog.info(LOG_LABEL, "getComponent popupPaddingLeft = " + popupPaddingLeft
                    + ", popupPaddingTop = " + popupPaddingTop);
            textView.setMarginsLeftAndRight(popupPaddingLeft, popupPaddingRight);
            textView.setMarginsTopAndBottom(popupPaddingTop, popupPaddingBottom);


            if (backgroundSelector != 0) {
                ShapeElement element = new ShapeElement();
                element.setRgbColor(RgbColor.fromArgbInt(backgroundSelector));
                textView.setBackground(element);
            }
            Configuration config = context.getResourceManager().getConfiguration();
            if (config.isLayoutRTL) {
                textView.setLayoutDirection(Component.LayoutDirection.RTL);
            }
            convertView.setTag(new ViewHolder(textView));
        } else {
            textView = ((ViewHolder) convertView.getTag()).textView;
        }
        textView.setText(getItemText(position));
        return convertView;
    }

    /**
     * getItemText
     *
     * @param position 位置信息
     * @return ItemText
     */
    public String getItemText(int position) {
        return getItem(position).toString();
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    /**
     * notifyItemSelected
     *
     * @param index
     * @javadoc notifyItemSelected
     */
    public void notifyItemSelected(int index) {
        selectedIndex = index;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public abstract T getItem(int position);

    @Override
    public abstract int getCount();

    /**
     * get
     *
     * @param position
     * @return T
     */
    public abstract T get(int position);

    /**
     * getItems
     *
     * @return List
     */
    public abstract List<T> getItems();

    public void setHintEnabled(boolean ishintenabled) {
        this.isHintEnabled = ishintenabled;
    }

    public boolean isHintEnabled() {
        return this.isHintEnabled;
    }

    /**
     * setTextColor
     *
     * @param textColor
     * @return MaterialSpinnerBaseAdapter
     */
    public MaterialSpinnerBaseAdapter<T> setTextColor(int textColor) {
        this.textColor = textColor;
        return this;
    }

    /**
     * setTextSize
     *
     * @param textSize
     * @return MaterialSpinnerBaseAdapter
     */
    public MaterialSpinnerBaseAdapter<T> setTextSize(int textSize) {
        this.textSize = textSize;
        return this;
    }

    /**
     * setBackgroundSelector
     *
     * @param backgroundSelector
     * @return MaterialSpinnerBaseAdapter
     */
    public MaterialSpinnerBaseAdapter<T> setBackgroundSelector(int backgroundSelector) {
        this.backgroundSelector = backgroundSelector;
        return this;
    }

    /**
     * setPopupPadding
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @return MaterialSpinnerBaseAdapter
     */
    public MaterialSpinnerBaseAdapter<T> setPopupPadding(int left, int top, int right, int bottom) {
        HiLog.info(LOG_LABEL, "setPopupPadding popupPaddingLeft = "
                + popupPaddingLeft + ", popupPaddingTop = " + popupPaddingTop);
        this.popupPaddingLeft = left;
        this.popupPaddingTop = top;
        this.popupPaddingRight = right;
        this.popupPaddingBottom = bottom;
        return this;
    }

    /**
     * ViewHolder
     *
     * @since 2021-04-22
     */
    private static class ViewHolder {
        private Text textView;

        private ViewHolder(Text textView) {
            this.textView = textView;
        }
    }
}